package com.oak.broke.model.vip.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oak.broke.constant.VipConstant;
import com.oak.broke.model.vip.service.ITsDmService;
import com.oak.broke.model.vip.service.IVipApplyService;
import com.oak.broke.model.vip.service.IVipApplyUserService;
import com.oak.broke.model.vip.dao.VipApplyDao;
import com.oak.broke.model.vip.dto.VipApplyDTO;
import com.oak.broke.model.vip.entity.VipApplyEntity;
import com.oak.broke.model.vip.entity.VipApplyUserEntity;
import com.oak.broke.model.vip.vo.VipApplyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 会员申请
 *
 * @author guoxy
 * @email broke@gmail.com
 * @date 2023-05-18 10:37:20
 */
@Service("vipApplyService")
@Slf4j
public class VipApplyServiceImpl extends ServiceImpl<VipApplyDao, VipApplyEntity> implements IVipApplyService {

    @Autowired
    private IVipApplyUserService vipApplyUserService;

    @Autowired
    private ITsDmService tsDmService;

    @Override
    public IPage<VipApplyVO> listVipApply(VipApplyDTO dto) {
        IPage<VipApplyVO> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        page = this.baseMapper.vipApplyList(page, dto);
        page.getRecords().forEach(item -> {
            if (item.getVipExpireDate() == null) {
                item.setIsVip(VipConstant.VIP_STATUS_NO);
            } else {
                // 当前日期-会员到期时间
                long dates = DateUtil.between(DateUtil.date(), item.getVipExpireDate(), DateUnit.DAY,false);
                item.setIsVip(dates < 0 ? VipConstant.VIP_STATUS_NO : VipConstant.VIP_STATUS_YES);
            }
            item.setIsVipName(VipConstant.VIP_STATUS_YES.equals(item.getIsVip())?VipConstant.VIP_YES:VipConstant.VIP_NO);
            String applyType = tsDmService.getTsdmValByKindAndCode(VipConstant.VIP_APPLY_TYPE_TS_DM_KIND, item.getApplyType());
            item.setApplyType(applyType);
            String applyFinish = tsDmService.getTsdmValByKindAndCode(VipConstant.VIP_APPLY_FINISH_TS_DM_KIND, item.getIsFinish());
            item.setIsFinish(applyFinish);
        });
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveApply(VipApplyDTO dto) {
        long start = System.currentTimeMillis();
        log.info("开始执行保存会员申请任务");
        VipApplyEntity vipApplyEntity = getVipApply(dto);
        if (ObjectUtil.isEmpty(vipApplyEntity)) {
            vipApplyEntity = new VipApplyEntity();
            // 申请表信息
            BeanUtil.copyProperties(dto, vipApplyEntity, false);
            vipApplyEntity.setCreateUserCode(dto.getUserCode());
            vipApplyEntity.setUpdateUserCode(dto.getUserCode());
            vipApplyEntity.setCreateTime(DateUtil.date());
            vipApplyEntity.setUpdateTime(DateUtil.date());
            this.saveOrUpdate(vipApplyEntity);
        }
        // 保存申请用户信息
        VipApplyUserEntity vipApplyUserEntity = new VipApplyUserEntity();
        BeanUtil.copyProperties(dto, vipApplyUserEntity, false);
        vipApplyUserEntity.setVipApplyId(vipApplyEntity.getVipApplyId());
        vipApplyUserEntity.setApplyTime(DateUtil.date());
        vipApplyUserEntity.setIsFinish(VipConstant.VIP_APPLY_FINISH_NO);
        vipApplyUserEntity.setCreateUserCode(dto.getUserCode());
        vipApplyUserEntity.setUpdateUserCode(dto.getUserCode());
        vipApplyUserEntity.setCreateTime(DateUtil.date());
        vipApplyUserEntity.setUpdateTime(DateUtil.date());
        vipApplyUserService.save(vipApplyUserEntity);
        // 更新最新申请的用户记录
        vipApplyEntity.setVipApplyUserId(vipApplyUserEntity.getVipApplyUserId());
        this.saveOrUpdate(vipApplyEntity);
        log.info("保存会员申请任务结束，共耗时:{}毫秒", (System.currentTimeMillis() - start));
    }

    private VipApplyEntity getVipApply(VipApplyDTO dto) {
        LambdaQueryWrapper<VipApplyEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VipApplyEntity::getUserRelationCode, dto.getUserRelationCode());
        queryWrapper.eq(VipApplyEntity::getApplyType, dto.getApplyType());
        queryWrapper.eq(VipApplyEntity::getVipUserType, dto.getVipUserType());
        return this.getOne(queryWrapper);
    }
}
